package com.go4thx.util;

import java.util.Calendar;

import com.go4thx.table.day.DayPanel;


public class DayProvider {
	
	private ChineseCalendar[] acc;
	private ChineseCalendar today;
	private int localYear;
	private int localMonth;
	private int localDay;
	private int dutyDiff = 3;
	
	public DayProvider(){
		Calendar c = Calendar.getInstance();
		localYear = c.get(Calendar.YEAR);
		localMonth = c.get(Calendar.MONTH);
		localDay = c.get(Calendar.DATE);
		this.setToday(new ChineseCalendar(localYear, localMonth, localDay, dutyDiff));
	}
	
//	public DayProvider(int dutyDiff) {
//		this.dutyDiff = dutyDiff;
//		Calendar c = Calendar.getInstance();
//		localYear = c.get(Calendar.YEAR);
//		localMonth = c.get(Calendar.MONTH);
//		this.setToday(new ChineseCalendar(localYear, localMonth, 1, dutyDiff));
//	}
	
	public DayPanel[][] getMonthData(){
		return getMonthData(localYear, localMonth);
	}
	
	public DayPanel[][] getMonthData(int y, int m){
		int totalDaysOfMonth = (new ChineseCalendar(y, m, 1)).getActualMaximum(Calendar.DATE);
		this.acc = new ChineseCalendar[totalDaysOfMonth];
		for (int i = 0; i < acc.length; i++) {
			acc[i] = new ChineseCalendar(y, m, i + 1, dutyDiff);
		}
		int firstDayOfWeek = acc[0].get(Calendar.DAY_OF_WEEK);
		
		DayPanel[][] DayPanel2D = new DayPanel[6][7];
		DayPanel[] DayPanel1D = new DayPanel[7];
		
		int weekIndex = 0;
		for (int i = firstDayOfWeek - 1; i < totalDaysOfMonth + firstDayOfWeek - 1; i++) {
			DayPanel1D[i % 7] = new DayPanel(acc[i - firstDayOfWeek + 1], today.toString());
			if (i % 7 == 6) {
				DayPanel2D[weekIndex] = DayPanel1D;
				DayPanel1D = new DayPanel[7];
				weekIndex++;
			}
		}
		DayPanel2D[weekIndex] = DayPanel1D;
		return DayPanel2D;
	}
	
	public ChineseCalendar getChineseCalendar(int y, int m){
		return new ChineseCalendar(y, m, 1);
	}

	public ChineseCalendar getToday() {
		return today;
	}

	public void setToday(ChineseCalendar today) {
		this.today = today;
	}

}
